﻿<!DOCTYPE>

<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=EDGE;IE=11;IE=8,chrome=1" />
    <title>设备排产计划详细查看</title>
    <link href="/InSiteWebApplication/Content/Easyui/themes/metro/easyui.css" rel="stylesheet" />
    <link href="/InSiteWebApplication/Content/Easyui/custom-easyui.css" rel="stylesheet" />
    <link href="/InSiteWebApplication/Content/Easyui/themes/icon.css" rel="stylesheet" />
   
</head>
<body>
    <div class="panelSection" style="display:none">    
        <div class="easyui-panel " title="查询区域" style="padding:3px;width: 100%" 
        data-options="closable:false,collapsible:true,minimizable:false,maximizable:false"> 
            <div style="margin:3px 0 0 3px">
                <div style="float:left;display:none" >
                    <div class="divLabel" style="margin-bottom: 5px">
                            设备：</div>
                        <input id="txtResource" class="easyui-textbox" name="Resource" type="text" data-options=""
                            style="width: 120px; height: 22px" />
                </div>
                <div style="float: left; margin-left: 10px">
                    <div class="divLabel" style="margin-bottom: 5px">
                        排产开始日期</div>
                    <input id="txtSrStartDate" type="text" class="easyui-datebox" style="height: 20px;
                        width: 148px" />
                </div>
                <div style="float: left; margin-left: 10px">
                    <div class="divLabel" style="margin-bottom: 5px">
                        排产结束日期</div>
                    <input id="txtSrEndDate" type="text" class="easyui-datebox" style="height: 20px;
                        width: 148px" />
                </div>
                <a href="#" id="btnSearch" class="easyui-linkbutton" data-options="width:80" style="margin-left:10px;margin-top: 15px;">
                            查询</a>
                <a href="#" id="btnReset" class="easyui-linkbutton" data-options="width:80" style="margin-left:10px;margin-top: 15px;">
                            重置</a>
                <div style="clear:both" ></div>
                 
            </div>
        </div>
    </div>
    <div class="panelSection">    
        <div class="easyui-panel " title="设备排产信息" style="padding:3px;width: 100%" 
        data-options="closable:false,collapsible:true,minimizable:false,maximizable:false"> 
               <div style="margin: 3px 0px 3px 3px;">
                    <table id="gdResAps" class="easyui-datagrid" style="width: 99%; height: 350px"
                        data-options="rownumbers:true,pagination:false,striped:true,checkOnSelect:true,selectOnCheck:true,singleSelect:true,pageNumber:1,pageSize:5,pageList:[5,10,20]">
                    </table>  
               </div>
        </div> 

    </div> 

    <div class="panelSection">    
        <div class="easyui-panel " title="工序排产详细信息" style="padding:3px;width: 100%" 
        data-options="closable:false,collapsible:true,minimizable:false,maximizable:false"> 
            <div style="margin:3px 0 0 3px">

                <div style="float:left">
                    <div style="float:left" >
                         <div class="divLabel" style="margin-bottom: 5px; ">
                                工区：</div>
                         <select id="ddlWorkcenter" class="easyui-combobox"  data-options="width:120,height:20 "></select>
                    </div>
                    <div style="float:left;margin-left:10px" >
                         <div class="divLabel" style="margin-bottom: 5px; ">
                                班组：</div>
                         <select id="ddlTeam" class="easyui-combobox"  data-options="width:120,height:20, panelHeight:'auto' "></select>
                    </div>
                    <div style="float:left;margin-left:10px" >
                         <div class="divLabel" style="margin-bottom: 5px; ">
                                设备：</div>
                         <select id="ddlResource" class="easyui-combobox"  data-options="width:120,height:20, panelHeight:'auto' "></select>
                    </div>

                    <div style ="clear:both" ></div>

                    <div style="float: left; margin-left: 0px">
                        <div class="divLabel" style="margin-bottom: 5px">
                            排产开始日期</div>
                        <input id="txtStartDate" type="text" class="easyui-datetimebox" style="height: 20px;
                            width: 148px" />
                    </div>
                    <div style="float: left; margin-left: 10px">
                        <div class="divLabel" style="margin-bottom: 5px">
                            排产结束日期</div>
                        <input id="txtEndDate" type="text" class="easyui-datetimebox" style="height: 20px;
                            width: 148px" />
                    </div>
                </div>
                                
                <div style="float: left; margin-left: 10px;width:300px">
                    <div id="gdMkEmpLabel" class="divLabel" style="margin-bottom: 5px">
                        加工人员</div>
                    <table id="gdMakeEmployee" class="easyui-datagrid" style="width: 100%; height: 150px"
                            data-options="rownumbers:true,pagination:false,striped:true,checkOnSelect:true,selectOnCheck:true,singleSelect:false,pageNumber:1,pageSize:5,pageList:[5,10,20]">
                        
                    </table> 
                </div>
                 <div style ="clear:both" ></div>
            </div>
        </div>
    </div>
    <div class="panelSection" style="display:none">    
        <div class="easyui-panel " title="按钮区域" style="padding:3px;width: 100%" 
        data-options="closable:false,collapsible:true,minimizable:false,maximizable:false"> 
            <a href="#" id="btnSaveChange" class="easyui-linkbutton" data-options="" style="margin-top: 0px;">
                            保存调整</a>
        </div>
    </div>

</body>
</html>
<script src="/InSiteWebApplication/Scripts/CustomPageJs/ReferenceJsLib.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/jquery.min.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/jquery.easyui.min.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/easyui-lang-zh_CN.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Scripts/CustomPageJs/CustomCommon.js" type="text/javascript"></script>
<script type="text/javascript">
    /*
    创建人：Wangjh
    日期：2022-04-28
    描述：设备排产甘特图详细
    */
    var page = {
        pagetotal: 0,
        pageSize: 50,
        pageNumber: 1,
        parentID: '', //父窗体div
        returnData: null, //返回的消息
        param: { resourceId: '0005d2800000003d', containDate: '2022-5-6' },
        rootPath: getRootPath(), //网站根路径
        ajaxUrl: getRootPath() + "/Ashx/ApsHandler/ApsResourceCapacityHandler.ashx",
        ajaxUrl2: getRootPath() + "/Ashx/ApsHandler/ApsLimitedCapacityHandler.ashx",
        //初始化页面
        init: function () {
            page.initSomeControl();
            page.bind();
            page.searchData();
            //customizedEasyuiPagination('pp'); //定制化分页
        },
        initSomeControl: function () {
            $("#ddlWorkcenter").combobox({
                panelHeight: 'auto',
                valueField: 'WORKCENTERID',
                textField: 'WORKCENTERNAME',
                url: page.ajaxUrl2 + "?action=getWorkcenter",
                onSelect: function (record) {
                    page.loadTeam(record.WORKCENTERID);
                }
            });

            $("#ddlTeam").combobox({
                panelHeight: 'auto',
                valueField: 'TEAMID',
                textField: 'TEAMNAME'
            });

            $("#ddlResource").combobox({
                panelHeight: 'auto',
                valueField: 'RESOURCEID',
                textField: 'RESNAME',
                onSelect: function (record) {
                    $.ajax({
                        type: "Get",
                        url: page.ajaxUrl2 + "?action=getResEmployee&resourceId=" + record.RESOURCEID,
                        cashe: false,
                        async: false, //同步
                        beforeSend: function () {

                        },
                        success: function (data) {
                            var result = JSON.parse(data);
                            $("#gdMakeEmployee").datagrid('loadData', { total: result.length, rows: result });
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest.responseText);
                        }
                    });
                },
                onLoadSuccess: function () {

                }
            });
        },
        loadTeam: function (workcenterId) {
            if (isNullOrWhiteSpaceStr(workcenterId)) {
                return;
            }
            //console.log('select workcenter'); 
            $("#ddlTeam").combobox('clear');
            $.ajax({
                type: "Get",
                url: page.ajaxUrl2 + "?action=getTeam&workcenterId=" + workcenterId,
                cashe: false,
                async: false, //同步
                beforeSend: function () {

                },
                success: function (data) {
                    $("#ddlTeam").combobox('loadData', JSON.parse(data))
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest.responseText);
                }
            });
            //$("#ddlTeam").combobox( 'reload', page.ajaxUrl+"?action=getTeam&workcenterId="+workcenterId );
        },
        loadResource: function (specid) {
            if (isNullOrWhiteSpaceStr(specid)) {
                return;
            }
            //console.log('select workcenter'); 
            $("#ddlResource").combobox('clear');

            $.ajax({
                type: "Get",
                url: page.ajaxUrl2 + "?action=getResourceBySpec&specId=" + specid,
                cashe: false,
                async: false, //同步
                beforeSend: function () {

                },
                success: function (data) {
                    $("#ddlResource").combobox('loadData', JSON.parse(data))
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest.responseText);
                }
            });
        }, 
        //页面事件绑定
        bind: function () {
            $("#gdMakeEmployee").datagrid({
                onLoadSuccess: function (data) {
                    for (var i = 0; i < data.rows.length; i++) {
                        if (data.rows[i].checked == true) {
                            $(this).datagrid('checkRow', i);
                        }
                    }
                },
                columns: [[
                    { field: 'ck', title: '选择', checkbox: true },
                    { field: 'EMPLOYEENAME', title: '员工号', width: 100 },
                    { field: 'FULLNAME', title: '姓名', width: 100 },
                    { field: 'EMPLOYEEID', title: 'EMPLOYEEID', hidden: true }
                ]]
            });
            $("#gdResAps").datagrid({
                onSelect: function (index, rowData) {
                    page.reset(false, false, true); //清除数据
                    page.loadStepDetail(rowData);
                },
                onLoadSuccess: function (data) {

                },
                columns: [[
                    { field: 'RESNAME', title: '设备', width: 180 },
                    { field: 'MFGORDERNAME', title: '订单', width: 120 },
                    { field: 'CONTAINERNAME', title: '批次', width: 180 },
                    { field: 'WORKFLOWSTEPNAME', title: '工序', width: 150 },
                    { field: 'WORKCENTERNAME', title: '工区', width: 100 },
		            { field: 'TEAMNAME', title: '班组', width: 100 },
		            { field: 'QTY', title: '数量', width: 60 },
		            { field: 'MAKEEMPCOUNTS', title: '作业人数', width: 60 },
		            { field: 'PREPARECYCLE', title: '准备时间(分)', width: 80 },
		            { field: 'MAKECYCLE', title: '加工时间(分)', width: 80 },
		            { field: 'TRANSFERCYCLE', title: '转运时间(分)', width: 80 },
		            { field: 'SHIPCYCLE', title: '转工时间(时)', width: 80 },
		            { field: 'COMMICYCLE', title: '外委时间(天)', width: 80 },
		            { field: 'APSTYPE', title: '排产策略', width: 60, formatter: function (value, row, index) {
		                switch (value) {
		                    case "1":
		                        return "正排";
		                        break;
		                    case "2":
		                        return "倒排";
		                        break;
		                    case "3":
		                        return "瓶颈排";
		                        break;
		                    default:
		                        return value;
		                        break;
		                }
		            }
		            },
		            { field: 'STEPSTARTTIME', title: '开始时间', width: 125 },
		            { field: 'STEPENDTIME', title: '结束时间', width: 125 },
		            { field: 'RESSTARTTIME', title: '设备开始时间', width: 125 },
		            { field: 'RESENDTIME', title: '设备结束时间', width: 125 },
                    { field: 'RESOURCEID', title: 'RESOURCEID', hidden: true },
                ]]
            });

            //查询按钮
            $('#btnSearch').on("click", function () {
                page.pageNumber = 1;
                page.reset(false, true, true);
                page.searchData();
            });

            //清除按钮
            $('#btnReset').on("click", function () {
                page.reset(true, true, true);
                page.pageNumber = 1; page.pagetotal = 0
                $('#pp').pagination('refresh', {
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
            });
            //调整
            $('#btnSaveChange').on("click", function () {
                page.LimitedCapacityChange();
            });

        },
        searchData: function () {
            var gridID = "#gdResAps";
            var param = {
                action: "getResourceApsGrid",
                pageSize: page.pageSize,
                pageNumber: page.pageNumber,
                //containerName: $("#txtContainer").textbox('getValue'),
                resourceId: page.param.resourceId,
                containDate: page.param.containDate,
                startDate: $("#txtSrStartDate").datebox('getValue'),
                endDate: $("#txtSrEndDate").datebox('getValue')
            };
            $.ajax({
                type: "Post",
                url: page.ajaxUrl,
                data: param,
                cashe: false,
                async: true,
                beforeSend: function () {
                    $(gridID).datagrid('loading');
                    $(this).attr("disabled", "disabled"); //禁用按钮
                },
                success: function (data) {
                    //console.log(data);
                    $(gridID).datagrid('loaded');
                    $(this).removeAttr("disabled"); //恢复按钮
                    var result = JSON.parse(data);
                    if (result.Result) {
                        //给分页对象赋值
                        page.pagetotal = result.DBTable.length;
                        $(gridID).datagrid('loadData', { total: page.pagetotal, rows: result.DBTable });
                    } else {
                        displayMessage(result.Message, false);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $(gridID).datagrid('loaded');
                    $(this).removeAttr("disabled"); //恢复按钮
                    alert(XMLHttpRequest.responseText);
                }
            });
        },
        loadStepDetail: function (row) {
            //$('#ddlWorkcenter').combobox('select', row.WORKCENTERID);

            $('#ddlWorkcenter').combobox('select', row.WORKCENTERID);
            $("#ddlTeam").combobox('select', row.TEAMID);
            //加载设备
            page.loadResource(row.SPECID);
            $("#ddlResource").combobox('select', row.RESOURCEID);
            //加载人员,已排产的人员排前面
            var data = $("#gdMakeEmployee").datagrid('getData').rows;
            var emps = row.EMPLOYEEIDS;
            var newData = [];
            var newData2 = [];
            var makeEmps = "";
            data.forEach(function (e) {
                if (emps != null && emps.indexOf(e.EMPLOYEEID) != -1) {
                    makeEmps += e.FULLNAME + "，";
                    e.checked = true;
                    newData.push(e);
                } else {
                    newData2.push(e);
                }
            });
            $("#gdMkEmpLabel").text("加工人员：" + makeEmps.trimEnd("，")); //label显示
            mergerNoRepeat(newData, newData2); //将newData2合并到newData中去 

            $("#gdMakeEmployee").datagrid('loadData', { total: data.length, rows: newData })
            //加载开始结束时间
            //console.log(row);
            $('#txtStartDate').datetimebox('setValue', row.STEPSTARTTIME);
            $('#txtEndDate').datetimebox('setValue', row.STEPENDTIME);
        },
        //清除方法，重要！！！
        reset: function (iscondition, isresult, isDetail) {
            clearMessage();
            if (iscondition) {
                $("#txtSrStartDate").datebox('clear');
                $("#txtSrEndDate").datebox('clear');
            }
            if (isresult) {
                clearEasyuiDataGrid("gdResAps", 1); //清空列表
            }
            if (isDetail) {
                //                $("#ddlWorkcenter").combobox('clear');//不生效
                //                $("#ddlTeam").combobox('clear');                               

                //$("#ddlWorkcenter").combobox('loadData',[]);
                //$("#ddlTeam").combobox('loadData',[]);
                $("#ddlWorkcenter").combobox('select', '');
                $("#ddlTeam").combobox('select', '');
                $("#ddlResource").combobox('loadData', []);
                $("#ddlResource").combobox('clear');
                $('#txtStartDate').datetimebox('clear');
                $('#txtEndDate').datetimebox('clear');
                clearEasyuiDataGrid("gdMakeEmployee", 1);
            }
        },
        //调整
        LimitedCapacityChange: function () {
            var activeRow = $("#gdApsContainerInfo").datagrid('getSelected');
            if (activeRow == null) {
                displayMessage("请选择批次", false);
                return;
            }
            var activeStepRow = $("#gdApsStepInfo").datagrid('getSelected');
            if (activeStepRow == null) {
                displayMessage("请选择工序", false);
                return;
            }

            var containers = [];
            var container = {
                containerid: activeRow.CONTAINERID,
                workflowid: activeRow.WORKFLOWID
            }
            if (activeRow.APSTYPE == "1") {
                container.startTime = $("#txtEndDate").datebox('getValue');
                container.endTime = activeRow.ENDTIME;
            } else if (activeRow.APSTYPE == "2") {
                container.endTime = $("#txtStartDate").datebox('getValue');
                container.startTime = activeRow.STARTTIME;
            } else if (activeRow.APSTYPE == "3") {
                container.startTime = activeRow.STARTTIME;
                container.endTime = activeRow.ENDTIME;
            }
            containers.push(container);

            var selectEmp = $("#gdMakeEmployee").datagrid('getChecked');

            if (parseInt(activeStepRow.MAKEEMPCOUNTS) != selectEmp.length) {
                displayMessage("所选工序作业人数和指定人数不一致", false);
                return;
            }

            var employeeIds = "";
            selectEmp.forEach(function (item) {
                employeeIds += item.EMPLOYEEID + ","
            });
            employeeIds = employeeIds.trimEnd(",");

            var postData = {
                action: "apsLmCalculateChange",
                apstype: activeRow.APSTYPE,
                apsplanDetailId: activeStepRow.APSPLANDETAILID,
                startTime: $("#txtStartDate").datebox('getValue'),
                endTime: $("#txtEndDate").datebox('getValue'),
                workcenterId: $("#ddlWorkcenter").combobox('getValue'),
                TeamId: $("#ddlTeam").combobox('getValue'),
                resourceId: activeStepRow.RESOURCEID,
                employeeIds: employeeIds,
                sequence: activeStepRow.SEQUENCE,
                containers: JSON.stringify(containers)
            }

            //startTime: $("#txtStartDate").datebox('getValue'), //txtStartDate
            //endTime: $("#txtEndDate").datebox('getValue'),

            $.ajax({
                type: "Post",
                url: page.ajaxUrl,
                data: postData,
                cashe: false,
                async: true,
                beforeSend: function () {
                    $.messager.progress({ text: '', msg: '处理中...' });
                },
                success: function (data) {
                    //console.log(data);
                    $.messager.progress('close');
                    var result = JSON.parse(data);
                    if (result.Result) {
                        page.searchData();
                        //父级标记
                        page.returnData = {
                            isParentRefresh: true
                        };
                        parent.$("#" + page.parentID).dialog('options').returnData = page.returnData;
                        displayMessage("调整成功", true);
                    } else {
                        displayMessage(result.Message, false);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.messager.progress('close');
                    alert(XMLHttpRequest.responseText);
                }
            });
        }
    }
    $(function () {
        page.parentID = getParam("parentDiv");
        page.param = parent.$("#" + page.parentID).dialog('options').popupData; 
        page.init();
    });
</script>